<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>商品</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-button type="primary" class="btn" @click="showadd()"
        >添加商品</el-button
      >
      <el-row>
        <el-table
          :data="
            product.slice((currentPage - 1) * pagesize, currentPage * pagesize)
          "
          border
          style="width: 100%"
        >
          <!-- type="index" -->
          <el-table-column label="id" width="80">
            <template slot-scope="scope">
              <span>{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column label="品牌">
            <template slot-scope="scope">
              <span>{{ scope.row.brand }}</span>
            </template>
          </el-table-column>
          <el-table-column label="纸种">
            <template slot-scope="scope">
              <span>{{ scope.row.type }}</span>
            </template>
          </el-table-column>
          <el-table-column label="级别">
            <template slot-scope="scope">
              <span text-align="center">{{ scope.row.rank1 }}</span>
            </template>
          </el-table-column>
          <el-table-column label="克重">
            <template slot-scope="scope">
              <span text-align="center">{{ scope.row.perweight }}</span>
            </template>
          </el-table-column>
          <el-table-column label="规格">
            <template slot-scope="scope">
              <span text-align="center">{{ scope.row.guide }}</span>
            </template>
          </el-table-column>
          <el-table-column label="单位">
            <template slot-scope="scope">
              <span text-align="center">{{ scope.row.unit }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="show(scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="remove(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="40"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="修改商品信息"
      :visible.sync="editdialogVisible"
      width="50%"
      :before-close="handleClose"
      @close="closed"
    >
      <el-form
        :model="addForm"
        :rules="addrules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="id" prop="id">
          <el-input v-model="addForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="addForm.brand"></el-input>
        </el-form-item>
        <el-form-item label="纸种" prop="type">
          <el-input v-model="addForm.type"></el-input>
        </el-form-item>
        <el-form-item label="级别" prop="rank">
          <el-input v-model="addForm.rank1"></el-input>
        </el-form-item>
        <el-form-item label="克重" prop="perweight">
          <el-input v-model="addForm.perweight"></el-input>
        </el-form-item>
        <el-form-item label="规格" prop="guide">
          <el-input v-model="addForm.guide"></el-input>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="addForm.unit"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="change">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="添加商品"
      :visible.sync="adddialogVisible"
      width="50%"
      :before-close="handleClose"
      @close="closed"
    >
      <el-form
        :model="addForm"
        :rules="addrules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="addForm.brand"></el-input>
        </el-form-item>
        <el-form-item label="纸种" prop="type">
          <el-input v-model="addForm.type"></el-input>
        </el-form-item>
        <el-form-item label="级别" prop="rank">
          <el-input v-model="addForm.rank1"></el-input>
        </el-form-item>
        <el-form-item label="克重" prop="perweight">
          <el-input v-model="addForm.perweight"></el-input>
        </el-form-item>
        <el-form-item label="规格" prop="guide">
          <el-input v-model="addForm.guide"></el-input>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="addForm.unit"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addproduct()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import axios from "axios";
import { getAll, upsert, removed } from "../../network/ProductManage.js";
import * as api from "@/network/ProductManage";
export default {
  data() {
    return {
      product: [],
      query: "",
      currentPage: 1,
      pagesize: 5,
      adddialogVisible: false,
      editdialogVisible: false,
      state: "正常",
      addForm: {
        id: "",
        brand: "",
        type: "",
        rank: "",
        perweight: "",
        guide: "",
        unit: "",
      },
      addrules: {
        id: [{ required: true, message: "请输入id", trigger: "blur" }],
        brand: [{ required: true, message: "请输入品牌", trigger: "blur" }],
        type: [{ required: true, message: "请输入纸种", trigger: "blur" }],
        rank1: [{ required: true, message: "请输入级别", trigger: "blur" }],
        perweight: [{ required: true, message: "请输入克重", trigger: "blur" }],
        guide: [{ required: true, message: "请输入规格", trigger: "blur" }],
        unit: [{ required: true, message: "请输入单位", trigger: "blur" }],
      },
    };
  },
  created() {
    getAll()
      .then((res) => {
        var result = res.data.obj;
        this.product = result;
        console.log(res.data.obj);
      })
      .catch(function (error) {
        console.log(error);
      });
  },
  methods: {
    change() {
      upsert(this.addForm)
        .then((res) => {})
        .then((response) => {
          console.log(response);
        })
        .catch(function (error) {
          console.log(error);
        });
      this.editdialogVisible = false;
    },
    handleEdit(id) {
      api.getById(id).then((res) => {
        this.formData = res.data;
        this.title = "编辑";
        this.editdialogVisible = true;
      });
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    getlist() {},
    handleSizeChange(val) {
      this.pagesize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    closed() {
      this.$refs.addFormRef.resetFields();
    },
    show(item) {
      this.editdialogVisible = true;
      this.addForm = JSON.parse(JSON.stringify(item));
    },
    showadd() {
      this.addForm = {
        brand: "",
        type: "",
        rank: "",
        perweight: "",
        guide: "",
        unit: "",
      };
      this.adddialogVisible = true;
    },
    remove(id) {
      //  console.log(id)
      removed({ id: id })
        .then((res) => {
          if (res.data.code == "200") {
            this.$message({
              message: res.data.status,
              type: "success",
            });
            getAll()
              .then((res) => {
                var result = res.data.obj;
                this.product = result;
                console.log(res.data.obj);
              })
              .catch(function (error) {
                console.log(error);
              });
          } else {
            this.$message(res.data.status);
          }
        })
        .catch((error) => {
          console.log(error); // 异常
        });
    },
    addproduct() {
      upsert(this.addForm)
        .then((res) => {
          if (res.data.code == "200") {
            this.$message({
              message: res.data.status,
              type: "success",
            });
            this.adddialogVisible = false;
            getAll()
              .then((res) => {
                var result = res.data.obj;
                this.product = result;
                console.log(res.data.obj);
              })
              .catch(function (error) {
                console.log(error);
              });
          } else {
            this.$message(res.data.status);
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
  },
};
</script>

<style>
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 12px;
}
.btn {
  margin-left: 880px;
}
.el-table {
  margin-top: 15px;
}
.el-pagination {
  text-align: center;
}
</style>
